﻿--[[
   @brief    model for contact list ui
   @author   perpu
   @version  1.0
   @date     2016-06-15
--]]

-- all the index is zero based

contact_list_model =
{
    b_discussion_cllapsed_ = true;--记录讨论组是否收起。
    b_group_cllapsed_ = false;--记录群组分组是否收起。
    cache_friend_info = {};

    virtual_node_id_to_group_info = {};
    current_selected_group_id = "";
    current_selected_group_id = "";

    NODE_TYPE_DISCUSSION_TITLE = 1;
    NODE_TYPE_DISCUSSION_NODE = 2;
    NODE_TYPE_GROUP_TITLE = 3;
    NODE_TYPE_GROUP_NODE = 4;
    NODE_TYPE_NO_DISCUSS_TIPS = 5;
    NODE_TYPE_NO_GROUP_TIPS = 6;

    get_user_level_done = false;
    social_user_level = 0;
    max_mode_msg_list_tips_id = 0;
    max_mode_contact_list_tips_id = 0;
    min_mode_msg_list_tips_id = 0;
    min_mode_contact_list_tips_id = 0;

    get_group_list_done = false;
    mainframe_mode = 0; --默认大界面模式

    already_apply_join_group_ids = {};
    join_group_btn_id_to_group_id = {};

    NODE_HEIGHT = 60;

    insert_apply_join_group_id = function(g_id)
        table.insert(contact_list_model.already_apply_join_group_ids, g_id);
    end;
    
    is_apply_join_group_id = function(g_id)
        if #contact_list_model.already_apply_join_group_ids == 0 then
            return false;
        end
        for i, id in ipairs(contact_list_model.already_apply_join_group_ids) do
            if id == g_id then
                return true;
            end
        end
        return false;
    end;

    get_favor_discussion_count = function()
--        if contact_list_model.b_discussion_cllapsed_ == true then
--            return 0;
--        end;
        return group_list_model.get_favor_discussion_count();
    end;

    get_group_count = function()
--        if contact_list_model.b_group_cllapsed_ == true then
--            return 0;
--        end;
        return group_list_model.get_group_count_ext();
    end;

    get_discussion_info_by_index = function(index)
        return group_list_model.get_favor_discussion_group_info_by_index(index + 1);-- index是1开始的，不是zero based
    end;
    
    get_group_info_by_index = function(index)
        return group_list_model.get_normal_group_info_by_index(index + 1);-- index是1开始的，不是zero based
    end;

    init = function()
        ierd_tgp.tpf_ui.Log("[contact_list_model]init");
    end;

    uninit = function()
        ierd_tgp.tpf_ui.Log("[contact_list_model]uninit");
    end;

    get_item_info = function(index)
        --准备讨论组列表关键信息
        local disscussion_count = contact_list_model.get_favor_discussion_count();
        local discussion_tree_node = 0;
        local discussion_node_min = discussion_tree_node;
        local discussion_node_max = discussion_tree_node;
        local no_discussion_tips_node = 0;

        if disscussion_count > 0 then
            discussion_node_min = discussion_node_min + 1;
            discussion_node_max = discussion_node_min + disscussion_count - 1;
        end;

        --如果讨论组数量为0而且树被展开，就显示tips
        if disscussion_count == 0 and contact_list_model.b_discussion_cllapsed_ == false then 
            no_discussion_tips_node = discussion_node_max + 1;
        else
            no_discussion_tips_node = discussion_node_max;
        end;
        
        --准备群组列表关键信息
        local group_count = contact_list_model.get_group_count();
        local group_tree_node = no_discussion_tips_node + 1;
        local group_item_node_min = group_tree_node;
        local group_item_node_max = group_tree_node;
        local no_group_tips_node = 0;

        if group_count > 0 then
            group_item_node_min = group_item_node_min + 1;
            group_item_node_max = group_item_node_min + group_count - 1;
        end;

        --如果群组数量为0而且树被展开，就显示tips
        if group_count == 0 and contact_list_model.b_group_cllapsed_ == false then 
            no_group_tips_node = group_item_node_max + 1;
        else
            no_group_tips_node = group_item_node_max;
        end;

        --最后计算出信息
        local info = {};
        if index == discussion_tree_node then
            info['cllapsed'] = contact_list_model.b_discussion_cllapsed_;
            return contact_list_model.NODE_TYPE_DISCUSSION_TITLE, "social\\contact_discussion_title_node.tpf",info, 25;

        elseif index > discussion_tree_node and index <= discussion_node_max then
            info['name'] = tostring(index - discussion_node_min);
            discuss_group_info = contact_list_model.get_discussion_info_by_index(index - discussion_node_min);
            if discuss_group_info ~= nil then
                info['group_info'] = discuss_group_info;
            end;
            return contact_list_model.NODE_TYPE_DISCUSSION_NODE, "social\\contact_group_node.tpf",info, 58;

        elseif index > discussion_node_max and index <= no_discussion_tips_node then
            return contact_list_model.NODE_TYPE_NO_DISCUSS_TIPS, "social\\contact_no_discussion_tips.tpf",{}, 105;

        elseif index == group_tree_node then
            info['cllapsed'] = contact_list_model.b_group_cllapsed_;
            return contact_list_model.NODE_TYPE_GROUP_TITLE, "social\\contact_group_title_node.tpf",info, 25;

        elseif index > group_tree_node and index <= group_item_node_max then
            info['name'] = tostring(index - group_item_node_min);
            group_info = contact_list_model.get_group_info_by_index(index - group_item_node_min);
            if group_info ~= nil then
                info['group_info'] = group_info
            end;
            return contact_list_model.NODE_TYPE_GROUP_NODE, "social\\contact_group_node.tpf",info, 58;

        elseif index > group_item_node_max and index <= no_group_tips_node then            
            return contact_list_model.NODE_TYPE_NO_GROUP_TIPS, "social\\contact_no_group_tips.tpf",info, 325;
        end;
    end;

    get_group_item_info = function (index)
        local group_count = contact_list_model.get_group_count();
        if 0 == group_count and 0 == index then
            -- return contact_list_model.NODE_TYPE_NO_GROUP_TIPS, "social\\contact_no_group_tips.tpf",info, 325;
            return nil;
        else
            if index >= 0 and index < group_count then
                local info = {};
                info['name'] = tostring(index);
                group_info = contact_list_model.get_group_info_by_index(index);
                if group_info ~= nil then
                    info['group_info'] = group_info
                end;
                return contact_list_model.NODE_TYPE_GROUP_NODE, "social\\contact_group_node.tpf", info, contact_list_model.NODE_HEIGHT;
            end
        end
    end;

    get_discuss_item_info = function (index)
        local disscussion_count = contact_list_model.get_favor_discussion_count();
        if 0 == disscussion_count and 0 == index then
            -- return contact_list_model.NODE_TYPE_NO_DISCUSS_TIPS, "social\\contact_no_discussion_tips.tpf", {}, 105;
            return nil;
        else
            if index >= 0 and index < disscussion_count then
                local info = {};
                info['name'] = tostring(index);
                discuss_group_info = contact_list_model.get_discussion_info_by_index(index);
                if discuss_group_info ~= nil then
                    info['group_info'] = discuss_group_info;
                end;
                return contact_list_model.NODE_TYPE_DISCUSSION_NODE, "social\\contact_group_node.tpf", info, contact_list_model.NODE_HEIGHT;
            end
        end
    end;

    get_total_item_height = function()
        local height = 0;
        height = height + 25; --讨论组分组按钮

        local disscussion_count = contact_list_model.get_favor_discussion_count();
        height = height + 58 * disscussion_count; --讨论组
        
        if disscussion_count == 0 and contact_list_model.b_discussion_cllapsed_ == false then 
            --如果讨论组数量为0而且树被展开，就显示广告
            height = height + 105; --讨论组广告
        else
        end;
        
        height = height + 25; --群组分组按钮
        local group_count = contact_list_model.get_group_count();
        height = height + 58 * group_count; --群组节点

        if group_count == 0 and contact_list_model.b_group_cllapsed_ == false then
            height = height + 325;
        else
        end

        return height;
    end;

    get_group_total_item_height = function()
        local height = 0;
        local group_count = contact_list_model.get_group_count();
        height = height + contact_list_model.NODE_HEIGHT * group_count; --群组节点

        return height;
    end;

    get_discuss_total_item_height = function()
        local height = 0;
        local group_count = contact_list_model.get_favor_discussion_count();
        height = height + contact_list_model.NODE_HEIGHT * group_count; --讨论组节点

        return height;
    end;

    first_visible_item_index = function(scrollbar_pos)
        --准备讨论组列表关键信息
        local disscussion_count = contact_list_model.get_favor_discussion_count();
        local discussion_tree_node = 0;
        local discussion_node_min = discussion_tree_node;
        local discussion_node_max = discussion_tree_node;
        local no_discussion_tips_node = 0;

        if disscussion_count > 0 then
            discussion_node_min = discussion_node_min + 1;
            discussion_node_max = discussion_node_min + disscussion_count - 1;
        end;

        if disscussion_count == 0 and contact_list_model.b_discussion_cllapsed_ == false then 
            --如果讨论组数量为0而且树被展开，就显示tips
            no_discussion_tips_node = discussion_node_max + 1;
        else
            no_discussion_tips_node = discussion_node_max;
        end;
        
        --准备群组列表关键信息
        local group_count = contact_list_model.get_group_count();
        local group_tree_node = no_discussion_tips_node + 1;
        local group_item_node_min = group_tree_node;
        local group_item_node_max = group_tree_node;
        local no_group_tips_node = 0;

        if group_count > 0 then
            group_item_node_min = group_item_node_min + 1;
            group_item_node_max = group_item_node_min + group_count - 1;
        end;

        if group_count == 0 and contact_list_model.b_group_cllapsed_ == false then
            --如果讨论组数量为0而且树被展开，就显示tips
            no_group_tips_node = group_item_node_max + 1;
        else
            no_group_tips_node = group_item_node_max;
        end
        local i = 0;
        local height = 0;
        for i = 0, no_group_tips_node do
            if i == discussion_tree_node then
                height = height + 25;
            elseif i == no_discussion_tips_node and no_discussion_tips_node ~= discussion_node_max then 
                --如果no_discussion_tips_node等于discussion_node_max，说明广告位没生效
                height = height + 105
            elseif i == group_tree_node then
                height = height + 25;
            elseif i == no_group_tips_node and no_group_tips_node ~= group_item_node_max then
                height = height + 325;
            else
                height = height + 58;
            end;

            if height > scrollbar_pos then
                return i;
            end;
        end;
        return i;
    end;

    group_first_visible_item_index = function(scrollbar_pos)
        local height = 0;
        local i = 0;
        local max_node = contact_list_model.get_group_count() - 1;
        for i = 0, max_node do
            height = height + contact_list_model.NODE_HEIGHT;

            if height > scrollbar_pos then
                return i;
            end;
        end;
        return i;
    end;

    discuss_first_visible_item_index = function(scrollbar_pos)
        local height = 0;
        local i = 0;
        local max_node = contact_list_model.get_favor_discussion_count() - 1;
        for i = 0, max_node do
            height = height + contact_list_model.NODE_HEIGHT;

            if height > scrollbar_pos then
                return i;
            end;
        end;
        return i;
    end;

    offset_of_first_visible_item_index = function(first_visible_index)
        local offset = 0;
        for i = 0, first_visible_index - 1 do
            offset = offset + contact_list_model.NODE_HEIGHT;
        end;
        return offset;
    end;

    clicked_treenode = function()
        contact_list_model.b_discussion_cllapsed_ = not contact_list_model.b_discussion_cllapsed_;
    end;
    clicked_grouptreenode = function()
        contact_list_model.b_group_cllapsed_ = not contact_list_model.b_group_cllapsed_;
    end;
};

contact_list_model.init();
